﻿using BaseUi.ViewModel;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace BaseUi
{
    /// <summary>
    /// 角色信息
    /// </summary>
    public partial class RoleEditView : Window
    {
        public RoleEditView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>true:成功，false:取消</returns>
        public static bool Show(RoleBase roleBase, CrudEnum crudEnum)
        {
            bool isOk = false;

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                var view = new RoleEditView();
                List<RoleEditTreeViewModel> dataList = new List<RoleEditTreeViewModel>();

                try
                {
                    if (crudEnum == CrudEnum.Read)
                        view.qr.IsEnabled = false;

                    view.textBox.Text = roleBase.Name;
                    dataList = RoleEditTreeViewModel.GetTreeViewModel(roleBase.Auths);
                    view.treeView.ItemsSource = dataList;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    view = null;
                    return;
                }

                view.qr.Click += (s, e) =>
                {
                    try
                    {
                        var treeNode = dataList.ToTreeNode();
                        RoleBase newRole = new RoleBase()
                        {
                            Id = roleBase.Id,
                            IsAdmin = roleBase.IsAdmin,
                            Name = view.textBox.Text.Trim(),
                            Auths =  treeNode.Where(o => o.IsSelect).Select(o => (int)o.Id).ToList(),
                            Time = roleBase.Time,
                        };

                        if (crudEnum == CrudEnum.Update)
                        {
                            if (string.IsNullOrEmpty(newRole.Name))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }
                            if (AuthDb.db.Queryable<RoleBase>().Where(o => o.Id != newRole.Id && o.Name == newRole.Name).Any())
                            {
                                MessageBox.Show("此存名称已存在");
                                return;
                            }

                            AuthDb.db.Updateable(newRole).ExecuteCommand();
                        }
                        else if (crudEnum == CrudEnum.Create)
                        {
                            newRole.Id = 0;
                            newRole.Time = DateTime.Now;

                            if (string.IsNullOrEmpty(newRole.Name))
                            {
                                MessageBox.Show("请输入名称");
                                return;
                            }
                            if (AuthDb.db.Queryable<RoleBase>().Where(o => o.Name == newRole.Name).Any())
                            {
                                MessageBox.Show("此存名称已存在");
                                return;
                            }

                            AuthDb.db.Insertable(newRole).ExecuteCommand();
                        }
                        else
                        {
                            MessageBox.Show("不支持此操作");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }

                    isOk = true;
                    view.Close();
                };

                view.ShowDialog();
                view = null;
            }));

            return isOk;
        }

        private void qx(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
